<?php
class LoginAction extends Action{
	public function __construct(){
		$autoload=$_COOKIE['isautoload'];
		if($autoload=='autoload'){
			$user=$_COOKIE['UserName'];
			$pwd=$_COOKIE['Password'];
			$res=M('users')->query('SELECT 
				UserId,UserName,Password,SchoolId,OrgName,TrueName,PhoneNumber,Email,UserType,IsApproved,LoginTime,LastLoginDate,Avatar,IsJury
				FROM `users` WHERE ( UserName="'.$user.'" AND Password="'.$pwd.'" )
				LIMIT 1');
			if($res){				
				$_SESSION[C('USER_AUTH_KEY')]=$res[0];
				$_SESSION[C('USER_AUTH_KEY')]['isload']=1;
			}
		}
	}
	/*
	登录页面*/
	public function showlogin(){
		$this->display();
	}
	/*
	登录身份判断 自动登录*/
	public function loginCheck(){
		if($_SESSION['verify']!=md5($this->_post('verify'))){
			$this->error('验证码错误');
		}
		$re=$this->login($this->_post("save"));
		switch($re){
			case 1:
				if($_SESSION[C('USER_AUTH_KEY')]['UserType']==1){
				$this->redirect('./Student/Home/index');
					break;
				}else if($_SESSION[C('USER_AUTH_KEY')]['UserType']==2){
					$this->redirect('./Student/Home/index');
					break;
				}else if($_SESSION[C('USER_AUTH_KEY')]['UserType']==3){
					$this->redirect('./Public/Index/index');
					break;
				}else if($_SESSION[C('USER_AUTH_KEY')]['UserType']==4){
					$this->redirect('./Public/Index/index');
					break;
				}else if($_SESSION[C('USER_AUTH_KEY')]['UserType']==5){
					$this->redirect('./Public/Index/index');
					break;
				}else{
					$this->redirect('./Public/Index/index');
					break;	
				}
			case 2:
				$this->error('密码错误,请重新输出',Index/login);	break;
			case 3:
				$this->error('用户正处于审核中，请等待！',Index/index);break;
			default:
				$this->error('登录失败,请重新登录','',3);
		}
    }
	/*
	登录信息检测
	修改登录次数 和登录时间
	*/
	 protected  function login($save=0){
		$res = M()->query('SELECT UserId,UserName,Password,SchoolId,OrgName,TrueName,PhoneNumber,Email,UserType,IsApproved,LoginTime,LastLoginDate,Avatar,IsJury FROM `users` WHERE ( UserName="'.$this->_post('UserName').'") LIMIT 1');
		if(md5($this->_post('Password')) == $res[0]['Password']){
			$_SESSION[C('USER_AUTH_KEY')]=$res[0];//登录成功，写入SESSION值
			if($save){
				$time=time()+3600*24*7;
				setcookie('isautoload','autoload',$time,'/');
				setcookie('UserName',$_SESSION[C('USER_AUTH_KEY')]['UserName'],$time,'/');
				setcookie('Password',$_SESSION[C('USER_AUTH_KEY')]['Password'],$time,'/');				
			}
			$LoginTime=$_SESSION[C('USER_AUTH_KEY')]['LoginTime']+1;
			$res=M('users')->execute("update __TABLE__ set LoginTime={$LoginTime},LastLoginDate=now() where UserId={$_SESSION[C('USER_AUTH_KEY')]['UserId']} limit 1");
			switch ($_SESSION[C('USER_AUTH_KEY')]['UserType']){
				case 3:
				case 4:
					if($_SESSION[C('USER_AUTH_KEY')]['IsApproved']==0){
						return 3;
					}
				default :
					return 1;
			}
		}else
			return 2;
    }
	//ajax的用户名检测 
	public function checkuname(){
		$user=$this->_post('UserName');
		$model=new Model();	
		// $res = $model->query('SELECT UserId,UserName,Password,SchoolId,OrgName,TrueName,PhoneNumber,Email,UserType,IsApproved,LoginTime,LastLoginDate,Avatar,IsJury FROM `users` WHERE ( UserName="'.$user.'") LIMIT 1');
		$res = $model->query('SELECT UserName FROM `users` WHERE ( UserName="'.$user.'") LIMIT 1');
		if(!$res){
			echo '<span style="color:red;font-size:10px"><img src="../../../Image/onError.gif" style="position:relative;top:6px;" /></span>';
		}else{
			echo '<span style="color:green;font-size:10px"><img src="../../../Image/onCorrect.gif" style="position:relative;top:6px;" /></span>';
		}
	}
	//用户退出
	public function loginOut(){
		setcookie ("isautoload", "", time()-3600,'/');
		$_COOKIE=array();
		unset($_COOKIE);
		
		unset($_SESSION[C('USER_PATH_KEY')]);
		setcookie(session_name(),null,time()-1,'/');
		$_SESSION=array();
		session_destroy();
			setcookie('UserName', null, time()-3600*24*365,'/');
			setcookie('Password', null, time()-3600*24*365,'/');
			setcookie('isautoload', null, time()-3600*24*365,'/');
		$this->redirect('Public/Index/index');
	}	
	Public function verify(){
		import('ORG.Util.Image');
		Image::buildImageVerify();
	}
}
?>